Optimize gtk_widget_path_copy() by preallocating "elems" array
authorMatthias Clasen <mclasen@redhat.com>
Tue, 7 Aug 2012 04:38:48 +0000 (00:38 -0400)
committerMatthias Clasen <mclasen@redhat.com>
Tue, 7 Aug 2012 04:38:48 +0000 (00:38 -0400)
gtk_widget_path_copy() currently calls g_array_append_val() in a loop,
which is inefficient due to reallocating the array's memory. Calling
g_array_set_size() before entering the loop reduces the number of CPU
cycles used by roughly 30%.

Patch by John Lindgren,
https://bugzilla.gnome.org/show_bug.cgi?id=679978

gtk/gtkwidgetpath.c

index 00afbe0b9e4a80c759c8460ab2190d6aac0e3797..855902fd6835e09ebb35d4f9659bb294ab692962 100644 (file)
@@ -174,15 +174,16 @@ gtk_widget_path_copy (const GtkWidgetPath *path)
 
   new_path = gtk_widget_path_new ();
 
+  g_array_set_size (new_path->elems, path->elems->len);
+
   for (i = 0; i < path->elems->len; i++)
     {
-      GtkPathElement *elem, new;
+      GtkPathElement *elem, *dest;
 
       elem = &g_array_index (path->elems, GtkPathElement, i);
+      dest = &g_array_index (new_path->elems, GtkPathElement, i);
 
-      gtk_path_element_copy (&new, elem);
-
-      g_array_append_val (new_path->elems, new);
+      gtk_path_element_copy (dest, elem);
     }
 
   return new_path;